﻿using MB.RuleBase.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Public;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WoChu.Job.Logic;

namespace Business
{
    public class OnlineStockBll
    {
        private static OnlineStockBll _NewInstance = new OnlineStockBll();

        public static OnlineStockBll NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new OnlineStockBll();
                }

                return _NewInstance;
            }
        }

        public class UnusualData
        {
            public string sku { set; get; }
            public string status { set; get; }
            public string deductionNum { set; get; }
            public string salebleNum { set; get; }
            public string outNum { set; get; }
        }

        public bool setOnlineStock(string josnData, string type, out string ReturnMsg)
        {
            ReturnMsg = "";
            try
            {
                bool switchOS = GetSwitch();
                if (switchOS)
                {
                    string url = ConfigurationManager.AppSettings["OnlineStock_url"];
                    if (type == "ON")
                    {
                        url += "api/ErpStock/InsertErpStock";
                        //url = "http://appapi.onlinestock.cn/api/ErpStock/InsertErpStock";
                        //url = "http://os.wochu.cn/api/ErpStock/InsertErpStock";
                    }
                    if (type == "OFF")
                    {
                        url += "api/StockImport/ERPWarehouseShelvesModel";
                        //url = "http://appapi.onlinestock.cn/api/StockImport/ERPWarehouseShelvesModel";
                        //url = "http://os.wochu.cn/api/StockImport/ERPWarehouseShelvesModel";
                    }
                    ClientChannel cc = new ClientChannel();
                    JObject result = JObject.Parse(cc.Post(josnData, url));
                    MB.Util.TraceEx.Write("同步在线系统地址：" + url);
                    MB.Util.TraceEx.Write("同步在线系统参数：" + josnData);
                    if (result.Value<bool>("hasError") == false)
                    {
                        if (type == "OFF")
                        {
                            if (result.Property("data") != null && result.Property("data").ToString() != "")
                            {
                                JArray pp = JArray.Parse(result.Property("data").Value.ToString());
                                if (pp.Count > 0)
                                {
                                    string EmailContent = "<table id=table1  cellspacing=1 ><tr style=text-align: center;><th style=font-size: large;color:red; border: 1px solid Gray; height:45px;width:80px>品项</th><th style=text-align: center;width:80px>下架数量</th><th style=text-align: center;width:80px >在线库存</th><th style=text-align: center;width:80px>超出数量</th></tr>";
                                    foreach (var ss in pp)
                                    {
                                        EmailContent += "<tr>";
                                        JObject datatmp = JObject.Parse(ss.ToString());
                                        string sku = datatmp.Value<string>("sku");
                                        EmailContent += "<td >" + sku + "</td>";
                                        string deductionNum = datatmp.Value<string>("deductionNum");//下架数量
                                        EmailContent += "<td >" + deductionNum + "</td>";
                                        string salebleNum = datatmp.Value<string>("salebleNum");//在线库存数
                                        EmailContent += "<td >" + salebleNum + "</td>";
                                        string outNum = datatmp.Value<string>("outNum");//超出下架数
                                        EmailContent += "<td >" + outNum + "</td>";
                                        EmailContent += "</tr>";
                                    }
                                    EmailContent += "</table>";
                                    EmailHelper.SendEmail("在线库存异常下架数据", EmailContent);
                                }
                            }
                        }
                        MB.Util.TraceEx.Write("同步在线系统成功：" + result);
                        return true;
                    }
                    else
                    {
                        string Eromsg = result.Value<string>("error").ToString();
                        MB.Util.TraceEx.Write("同步在线系统失败消息：" + Eromsg);
                        MB.Util.TraceEx.Write("同步在线系统失败参数：" + josnData);
                        return false;
                    }
                }
                else
                {
                    MB.Util.TraceEx.Write("同步在线系统开关已关闭！");
                    return true;
                }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write("同步在线问题消息：" + ex.Message);
                return false;
            }
        }

        //获取在线库存接口开关
        private bool GetSwitch()
        {
            try
            {
                bool SwitchOS = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<bool>("OrderCheck", "CheckOnlineStockSwitch").FirstOrDefault();

                return SwitchOS;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write("获取同步在线系统开关失败：" + ex.Message);
                return false;
            }
        }

    }
}
